// Purpose : This page display a list of messages posted by the patient to the doctor. The doctor can also reply for these.
// Date    : 24 Sep, 2007
// Author  : Vinayak Singh Rathore
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;
public partial class DashBoard_MyMessageBoard : System.Web.UI.Page
{
    // creating an instance of manager class
    Manager manager = new Manager(true,"ConnectionString");
    int DoctorId = 0;
    int userType = 0;
  //  int question = 0;
    
    protected string MessageUser = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Encryption objEncription = new Encryption();
        if (!Page.IsPostBack)
        {
                    PostMessae.Visible = false;

                    // CODE COMMENTED AS DENTIST WERE NOT ABLE TO ACCESS THIS PAGE
                    // DATE 18TH FEB 2008
                    
                //localFunction drchk = new localFunction();
                //drchk.checkDoctorLogin(true);

                    localFunction lfunction = new localFunction();
                    if (Session["DoctorType"].ToString() == "Doctor")
                    {
                        lfunction.checkDoctorLogin(true);
                        userType = 2;
                    }
                    else if (Session["DoctorType"].ToString() == "Dentist")
                    {
                        lfunction.checkDentistLogin(true);
                        userType = 4;
                    }
                    else
                        Response.Redirect(Session["strHttp"].ToString());


                if (Session["userID"].ToString() == "" && Session["userID"] == null)
                {
                    PostMessae.Visible = false;
                    MessageUser = "  !! Users please sign in to post.";
                }
              
                DoctorId = Convert.ToInt32(Session["userID"]);
                // doctorType = Convert.ToInt32(splitQuiry[1]);
                ///
                if (Session["userID"] == null || Session["userID"].ToString().Length <= 0)
                {
                     PostMessae.Visible = false;
                }

                //if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).Length == 0)))
                //userType = 2;
                //else if (Session["userType"].ToString() == "SiteUser")
                //userType = 3;
                //else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
                //userType = 4;

            // COMMENT ENDS

                //PostMessage1.TopicId = DoctorId;//set blogid value in TopicId property
               BindDoctorBlog();
        }

       
    }
   
    /// <summary>
    /// Call BindDoctorBlog() function after load the page 
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    {
        if (Manager.GetPostBackControl(Page) == "btnSubmit")
        {
            BindDoctorBlog();
        }
        base.Render(writer);
    }
    /// <summary>
    /// Bind Grid behalf Doctor ID
    /// </summary>
    protected void BindDoctorBlog()
    {
        DoctorId = Convert.ToInt32(Session["userID"]);
        //Show doctor name and his/her email id in Label

        string SQLBlog = string.Empty;
        if (Session["DoctorType"].ToString() == "Dentist")
            SQLBlog = "SELECT (FName+' '+MName+' '+LName) AS Name FROM tbl_Dentists WHERE id = " + DoctorId;
        else if((Session["DoctorType"].ToString() == "Doctor"))
            SQLBlog = "SELECT (FName+' '+MName+' '+LName) AS Name FROM tbl_Doctors WHERE id = " + DoctorId;

        
        DataSet dsShowBlog = manager.ExecuteQuery(SQLBlog, "tbl_Topics");
        if (dsShowBlog.Tables[0].Rows.Count > 0)
        {
            lblBlogName.Text = dsShowBlog.Tables[0].Rows[0][0].ToString();
           // lblDoctorName.Text = dsShowBlog.Tables[0].Rows[0][0].ToString();
        }

        string SQL = @"SELECT id, messageboard.msg AS msgDescription,messageboard.ason AS msgDate,
                    case messageboard.sendertype when 1 then (SELECT 'Admin') 
                    when 3 then (SELECT nickname from tbl_users WHERE id=Messageboard.senderid)
                    when 4 then (SELECT FName from tbl_dentists WHERE id=Messageboard.senderid)
                    when 2 then (SELECT FName from tbl_doctors WHERE id=Messageboard.senderid) 
                    end AS postedName FROM  Messageboard WHERE MessageBoard.RecieverID = " + Convert.ToInt32(Session["userID"]) + " Order By  messageboard.ason DESC";

        DataSet ds = manager.ExecuteQuery(SQL, "tbl_doctorMessage");
        if (ds.Tables[0].Rows.Count > 0)
        {
            grdDoctorBlog.DataSource = ds.Tables[0];
            grdDoctorBlog.DataBind();
            lblerr.Visible = false;
        }
        else
        {
            lblerr.Text = "<font color=red><b>No message posted currently !!!</b></font>";
            lblerr.Visible = true;
        }
        
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        Manager objManager = new Manager(true,"ConnectionString");
        if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).Length == 0)))
            userType = 2;
        else if (Session["userType"].ToString() == "SiteUser")
            userType = 3;
        else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            userType = 4;
        SqlParameter[] objSqlParameter = new SqlParameter[5];
        objSqlParameter[0] = new SqlParameter("@senderID", SqlDbType.BigInt);
        objSqlParameter[0].Value = Convert.ToInt32(Session["userID"]);
        objSqlParameter[1] = new SqlParameter("@sendertype", SqlDbType.BigInt);
        objSqlParameter[1].Value = userType;
        objSqlParameter[2] = new SqlParameter("@msg", SqlDbType.Text);
        objSqlParameter[2].Value = txtMessage.Text;
        objSqlParameter[3] = new SqlParameter("@ParentID",SqlDbType.BigInt);
        objSqlParameter[3].Value =Convert.ToInt32(ViewState["ParentID"]);
        objSqlParameter[4] = new SqlParameter("@IsError", SqlDbType.Bit);
        objSqlParameter[4].Direction = ParameterDirection.Output;
        objManager.ExecuteQuery("AddAnswerInMessageBoard", objSqlParameter);
        if (Convert.ToBoolean(objSqlParameter[4].Value))
        {
            lblerr.Text = "<font color=red><b>Resource not found !!!</b></font>";
            lblerr.Visible = true;
        }
        else
        {
            PostMessae.Visible = false;
            lblerr.Text = "<font color=red><b>Your Message have been posted successfully !!!</b></font>";
            lblerr.Visible = true;
        }



    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtMessage.Text = "";
        PostMessae.Visible = false;

    }
    protected void grdDoctorBlog_ItemCommand(object source, DataListCommandEventArgs e)
    {
       ViewState["ParentID"]=Convert.ToInt32(e.CommandArgument);
        PostMessae.Visible = true;
        
        BindDoctorBlog(); 
    }
}
